<?php
namespace Cyfy;  

/**
 * Constants.
 *
 * Variables, which can be set by modules themselves and stored in the database.
 *
 * @category cyfy
 * @author Dandelion <dandelion8888@gmail.com>
 * @version 0.1
 */
class Constants
{
    /**
     * Array of constants, filled on start
     * @var array()
     */
    static private $constants = array();
    
    /**
     * Loads constants from the database into array. Should be called on start of Cyfy.
     *
     */ 
    static public function load()
    {
        $result = \dibi :: query("SELECT [module],[name],[value] FROM [:cyfy:Cyfy-constants]");
        foreach($result as $constant)
        {
            self :: $constants[$constant["module"]][$constant["name"]] = $constant["value"];
        }	    
    }
  
    /**
     * Defines a new constant.
     *
     * If the name and module exist in the database, the value is replaced by the new one.
     *
     * @param string The module this constant belongs to.
     * @param string Constant name.
     * @param mixed
     * @retun bool
     */ 
    static public function define($module, $name, $value)
    {
        $values = array(
            'module' => $module,
            'name' => $name,
            'value' => $value
        );
        return \dibi :: query("REPLACE INTO [:cyfy:Cyfy-constants]", $values);
    }
    
    /**
     * Get a constant by module and constant name.
     *
     * @param string The module this constant belongs to.
     * @param string Constant name.
     * @return mixed
     *
     */ 
    static function get($module, $name)
    {
        return self :: $constants[$module][$name];
    }
  
    /**
     * Removes a constant.
     *
     * @param string The module this constant belongs to.
     * @param string Constant name. If not set, removes all constants of a module.
     * @return bool
     */ 
    static function remove($module, $name = '')
    {
        $values['module'] = $module;
        if($name)
        {
            $values['name'] = $name;
        }
        return dibi :: query("DELETE FROM [:cyfy:constants]", $values);
    } 
}


